(function(global){
        
    function _Observable() {
        this._handlers = {};    
    }
    
    _Observable.prototype = {
        /**
         * 注册事件.
         * @method on
         * @param type {String|Object}
         * @param handler {Function}
         * @return this
         */  
        on : function(type, handler) {        
            var _this = this;   
            if(Type.isUndefined(type)){
                return this;
            }     
            //兼容'add remove'注册同样的处理逻辑.   
            if(Type.isString(type)){
                var eventNames = type.replace(/\s+/g, ' ').split(' ');
                if(eventNames.length > 1){
                    for(var i=0; i<eventNames.length; i++){
                        this.on(eventNames[i], handler);
                    }
                }    
            }                  
            //兼容 {evt1:fun, evt2:fun}.
            if(Type.isObject(type)){
                var eventsMappingObj = type;
                Object.each(eventsMappingObj, function(eventName, eventHandler){
                    if(Type.isFunction(eventHandler)){
                        _this.on(eventName, eventHandler);
                    }
                });
            }        
            if( typeof this._handlers[type] === "undefined") {
                this._handlers[type] = [];
            }
            this._handlers[type].push(handler);
            return this;
        },    
        /**
         * 注销事件.
         * @method off
         * @param type {String}
         * @param handler {Function}
         * @return this
         */
        off : function(type, handler){
            if( typeof this._handlers[type] === "undefined") {
                return false;
            }
            this._handlers[type].remove(handler);
            return this;
        },
        /**
         * 触发事件.
         * @param type {String}
         * @return {Mixed} 事件逻辑执行的结果.
         */
        fireEvent : function(type) {
            var eventResults = [];        
            if( this._handlers[type] instanceof Array) {            
                var _handlers = this._handlers[type],
                    args = Array.prototype.slice.call(arguments, 1);
                                
                for (var i=0,len=_handlers.length; i<len; i++) {
                    eventResults.push(_handlers[i].apply(this, args));
                }
            }
            return eventResults.length == 1 ? eventResults[0] : eventResults;
        },
        /**
         * fireEvent的简写版本 1.
         */
        fire : function(){
            return this.fireEvent.apply( this, Array.prototype.slice.call(arguments));        
        },
        /**
         * fireEvent的简写版本 2.
         */
        trigger : function(){
            return this.fireEvent.apply( this, Array.prototype.slice.call(arguments));        
        }
    };
    
    //------------------------------------------------------------------------------------
    
    /**
     * 为obj或function注册事件机制.
     * @param obj {Object|Function}
     */
    _Observable.enable = function(obj/*or fun*/){
        if(typeof obj === 'function'){
            obj = obj.prototype; 
        }
        var observableObj = new _Observable();
        for(var key in observableObj){     
            obj[key] = observableObj[key];
        }
        return obj;
    };    
    /**
     * 创建Observable对象.
     */
    _Observable.create = function(parentObj){
        var newObj = {};
        _Observable.enable(newObj);
        if(Type.isObject(parentObj)){
            for(var member in parentObj){
                if(Type.isUndefined(newObj[member])){
                    newObj[member] = parentObj[member];
                }
            }
        }
        return newObj;  
    };    
    /**
     * 将config中指定的事件处理函数注册到obj上。
     * @param {Object} obj
     * @param {Object} config 
     * @param {Function} cb
     * 示例
     * {
     *     onClick : function(){},
     *     onOpen : function(){}
     * }
     */
    _Observable.regEvent = function(obj, config, cb){
        if(!$.isEmptyObject(config)){
            $.each(config, function(key, value){
                //规定以on开头的配置项为事件绑定.
                if(key.startsWith('on')){
                    obj.on( key.substr(2).lowercaseFirstChar(), value );
                }
            });
            if(Type.isFunction(cb)){
                cb.call(obj);
            }
        }
    };
    
    //------------------------------------------------------------------------------------
    
    //TODO 老版本兼容性代码，新项目中去掉.
    window.Observable = _Observable;
    
    ujs.define('Observable', 'ujs.evt', function(){
        return _Observable;
    });
    
})(window);